package com.ys.http.network;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MultipartBuilder {

    private MultipartBody.Builder builder;

    public MultipartBuilder() {
        builder = new MultipartBody.Builder();
    }

    public static MultipartBuilder newMultipart() {
        MultipartBuilder multipart = new MultipartBuilder();
        multipart.builder = new MultipartBody.Builder();
        return multipart;
    }


    public MultipartBuilder addParam(String name, String value) {
        if (isValidate(name, value)) {
            builder.addFormDataPart(name, value);
        }
        return this;
    }

    public MultipartBuilder addParam(String name, Number value) {
        if (isValidate(name)) {
            builder.addFormDataPart(name, String.valueOf(value));
        }
        return this;
    }

    public MultipartBuilder addParam(String name, boolean value) {
        if (isValidate(name)) {
            builder.addFormDataPart(name, String.valueOf(value));
        }
        return this;
    }

    public MultipartBuilder addParam(String name, String filename, RequestBody body) {
        if (isValidate(name)) {
            builder.addFormDataPart(name, filename, body);
        }
        return this;
    }

    public MultipartBuilder addPart(MultipartBody.Part part) {
        builder.addPart(part);
        return this;
    }

    public MultipartBuilder addPart(RequestBody body) {
        builder.addPart(body);
        return this;
    }

    public MultipartBuilder addPart(Headers headers, RequestBody body) {
        builder.addPart(headers, body);
        return this;
    }

    public MultipartBuilder setType(MediaType type) {
        builder.setType(type);
        return this;
    }

    public Body build() {
        return new Body(builder.build());
    }

    private boolean isValidate(String name) {
        return name != null;
    }

    private boolean isValidate(String name, String value) {
        return name != null && value != null;
    }
}
